package selfview.sufutian.com.myapplication.draw2d;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by sufutian on 2017/2/7.
 */

public class CanvasAnimiDraw extends View {

    private int mTotalWidth;
    private int mTotalHeight;
    private Paint mPaint;

    public CanvasAnimiDraw(Context context) {
        super(context);
        initPaint();
    }

    public CanvasAnimiDraw(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    public CanvasAnimiDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(new RectF(0, 0, mTotalWidth, mTotalHeight), mPaint);

        mPaint.setColor(Color.BLUE);
        mPaint.setTextSize(50);
        canvas.drawText("Canvas Translate:", 0, 50, mPaint);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(10, 60, 1070, 260, mPaint);


        canvas.save();
        mPaint.setTextSize(30);
        int count = 1070 - 10;
        for (int i = 0; i < count; i++) {
            if (i % 5 == 0) {
                canvas.drawText(String.valueOf((i / 5)), 0, 260 - 100, mPaint);
                canvas.drawLine(10, 260, 10, 260 - 100, mPaint);
            } else {
                canvas.drawLine(10, 260, 10, 260 - 50, mPaint);
            }
            canvas.translate(10, 0);

        }
        canvas.restore();

        mPaint.setTextSize(50);
        canvas.drawText("Canvas scale:", 0, 300, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(new Rect(0, 400, 400, 500), mPaint);
        canvas.scale(1.5f, 1.5f);
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(new Rect(0, 400, 400, 500), mPaint);
        canvas.save();

        canvas.restore();
        canvas.drawText("Canvas rotate:", 0, 550, mPaint);
        canvas.drawRect(new Rect(200, 600, 500, 700), mPaint);
        canvas.rotate(20);
        mPaint.setColor(Color.GREEN);
        canvas.drawRect(new Rect(200, 600, 500, 700), mPaint);
        canvas.save();


    }
}
